package sn.hn.demo.thread;

import sn.hn.demo.GameActivity;
import sn.hn.demo.GameSurface;
import sn.hn.demo.MainActivity;
import sn.hn.demo.unit.Bubbles;
import sn.hn.demo.unit.Cloud;
import sn.hn.demo.unit.Fish;

public class MyThreadFish extends Thread {
	GameSurface game;
	public static final int RF = 5;
	public static final int RB = MainActivity.SCREENWIDTH/100;
	public static final int RD = 2;
	public static boolean updateScore = false;
	public static int score = 0;
	public MyThreadFish(GameSurface game) {
		this.game = game;
	}

	@Override
	public void run() {
		super.run();
		while (!game.mFinish) {
			if(GameActivity.running)
			{
				try {
					Thread.sleep(40);
					updateMove();
					updateBubbles();
					updateFish();
					updateCloud();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void updateCloud() {
		if(game.mListCloud.size() <= 6)
		{
			Cloud c = new Cloud(GameActivity.mGameContext);
			game.mListCloud.add(c);
		}
		for(int i = 0; i < game.mListCloud.size(); i++)
		{
			if(game.mListCloud.get(i).isDie)
				game.mListCloud.remove(i);
			else
				game.mListCloud.get(i).moveCloud();
		}
	}
	
	public void updateMove() {
		if(game.mBoat.isMove)
		{
			if(!game.mFinish){
				if (game.mBoat != null) {
					if(game.mBoat.x == game.xEnd)
						game.mBoat.isMove = false;
					if(game.d > 0 )
					{ 
						if(game.xStart > game.xEnd) {
							if(game.mBoat.x > 0)
							{
								game.d -= RD;
								game.mBoat.x -= RB;
								game.mBoat.mSprite.xPos = game.mBoat.x;
							}
							if(game.mBoat.mSprite.xPos < 0){
								game.mBoat.moveBoat();
								game.mBoat.isMove = false;
							}
						}
						if (game.xStart < game.xEnd) {
							if(game.mBoat.x < MainActivity.SCREENWIDTH - game.mBoat.mBoat.getWidth())
							{
								game.d -= RD;
								game.mBoat.x += RB;
								game.mBoat.mSprite.xPos = game.mBoat.x;
							}
							if (game.mBoat.mSprite.xPos + game.mBoat.mBoat.getWidth() > MainActivity.SCREENWIDTH)
							{
								game.mBoat.moveBoat();
								game.mBoat.isMove = false;
							}
						}
					}else
						game.mBoat.isMove = false;
				}
			}
		}
	}


	public void updateBubbles() {
		if(game.mListBubbles.size() <= 5)
		{
			Bubbles b = new Bubbles(GameActivity.mGameContext);
			game.mListBubbles.add(b);
		}
		for(int i = 0; i < game.mListBubbles.size(); i++)
		{
			if(game.mListBubbles.get(i).isDie)
				game.mListBubbles.remove(i);
			else
				game.mListBubbles.get(i).moveBubbles();
		}
	}

	public void updateFish()
	{
		updateScore = false;
		score = 0;
		if (game.mListFishs.size() < game.mLevel.getLv_fish()) {
			Fish f = new Fish(GameActivity.mGameContext);
			game.mListFishs.add(f);
		}
		for (int i = 0; i < game.mListFishs.size(); i++) {
			if(game.mListFishs.get(i).y < game.mBoat.y + game.mBoat.mBoat.getHeight()){
				game.mListFishs.get(i).isCatched = true;
				game.isCatching = false;
			}
			if(!game.mListFishs.get(i).isCatched){
				if(!game.isCatching)
				{
					if(!game.mBoat.isLeft)
					{
						if(game.mListFishs.get(i).isLeft)
						{
							if((( game.mBoat.mSprite.xPos + 92*game.mBoat.mBoat.getWidth()/229 > game.mListFishs.get(i).mSprite.xPos )
									&& ( game.mBoat.mSprite.xPos + 92*game.mBoat.mBoat.getWidth()/229 < game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/16)) 
									&& (( game.y > game.mListFishs.get(i).y)
									&& ( game.y < game.mListFishs.get(i).y + game.mListFishs.get(i).mFish.getHeight())))
								{
									game.isFishing = false;
									game.isCatching = true;
									game.mListFishs.get(i).isUp = true;
								}
						}else{
							if(((game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/Fish.totalFrame > game.mBoat.mSprite.xPos + 143*game.mBoat.mBoat.getWidth()/229 )
									&& (game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/Fish.totalFrame -  game.mListFishs.get(i).mFish.getWidth()/16 < game.mBoat.mSprite.xPos + 143*game.mBoat.mBoat.getWidth()/229 )) 
									&& ((game.mListFishs.get(i).y < game.y)
							  		&& (game.mListFishs.get(i).y + game.mListFishs.get(i).mFish.getHeight() > game.y )))
								{
									game.isFishing = false;
									game.isCatching = true;
									game.mListFishs.get(i).isUp = true;
								}
						}
					}
					else{
						if(game.mListFishs.get(i).isLeft)
						{
							if((( game.mBoat.mSprite.xPos + 92*game.mBoat.mBoat.getWidth()/229 > game.mListFishs.get(i).mSprite.xPos )
									&& ( game.mBoat.mSprite.xPos + 92*game.mBoat.mBoat.getWidth()/229 < game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/16)) 
									&& (( game.y > game.mListFishs.get(i).y)
									&& ( game.y < game.mListFishs.get(i).y + game.mListFishs.get(i).mFish.getHeight())))
								{
									game.isFishing = false;
									game.isCatching = true;
									game.mListFishs.get(i).isUp = true;
								}
						}else{
							if(((game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/Fish.totalFrame > game.mBoat.mSprite.xPos + 143*game.mBoat.mBoat.getWidth()/229 )
									&& (game.mListFishs.get(i).mSprite.xPos + game.mListFishs.get(i).mFish.getWidth()/Fish.totalFrame -  game.mListFishs.get(i).mFish.getWidth()/16 < game.mBoat.mSprite.xPos + 143*game.mBoat.mBoat.getWidth()/229 )) 
									&& ((game.mListFishs.get(i).y < game.y)
							  		&& (game.mListFishs.get(i).y + game.mListFishs.get(i).mFish.getHeight() > game.y )))
								{
									game.isFishing = false;
									game.isCatching = true;
									game.mListFishs.get(i).isUp = true;
								}
						}
					}
				}
				if(game.mListFishs.get(i).isUp)
				{
					game.mListFishs.get(i).mSprite.init(game.mListFishs.get(i).mFishUp, game.mListFishs.get(i).mFishUp.getWidth()/8, game.mListFishs.get(i).mFishUp.getHeight(), 8, 8);
				}
			}else{
				game.mScore += game.mListFishs.get(i).score;
				updateScore = true;
				score = game.mListFishs.get(i).score;
				game.mListFishs.remove(i);
				if(game.mScore >= game.mLevel.getLevel_score())
				{
					game.mFinish = true;
				}
			}
			if (!game.mListFishs.get(i).die) {
				if (game.mListFishs.get(i).isUp) {
					if(!game.mBoat.isLeft)
					{
						game.mListFishs.get(i).x = game.mBoat.mSprite.xPos + 143*game.mBoat.mBoat.getWidth()/229 - game.mListFishs.get(i).mFishUp.getWidth()/16;
						game.mListFishs.get(i).y = game.y - 3;
						game.mListFishs.get(i).mSprite.xPos = game.mListFishs.get(i).x;
						game.mListFishs.get(i).mSprite.yPos = game.mListFishs.get(i).y;
					}else {
						game.mListFishs.get(i).x = game.mBoat.mSprite.xPos + 92*game.mBoat.mBoat.getWidth()/229 - game.mListFishs.get(i).mFishUp.getWidth()/16;
						game.mListFishs.get(i).y = game.y - 3;
						game.mListFishs.get(i).mSprite.xPos = game.mListFishs.get(i).x;
						game.mListFishs.get(i).mSprite.yPos = game.mListFishs.get(i).y;
					}
				} else {
					game.mListFishs.get(i).moveFish();
				}
			} else {
				game.mListFishs.remove(i);
			}
		}
	}
}
